﻿namespace InfluxDB.Flurl
{
    public class PointData
    {
        #region own

        public string Measurement { get; set; }

        public Dictionary<string, string> Tags { get; set; }

        public Dictionary<string, object> Fields { get; set; }

        public WritePrecision Precision { get; set; } = WritePrecision.Ns;

        public DateTimeOffset? Time { get; set; }

        #endregion

        public PointData()
        {
            Time = DateTimeOffset.UtcNow;
        }

        public PointData(string measurement)
        {
            Measurement = measurement;
            Time = DateTimeOffset.UtcNow;
        }

        public PointData(string measurement, DateTimeOffset? timeUtc)
        {
            Measurement = measurement;
            if (timeUtc.HasValue)
            {
                Time = timeUtc;
            }
            else
            {
                Time = DateTimeOffset.UtcNow;
            }
        }

        public void AddTag(string name, string value)
        {
            if (string.IsNullOrEmpty(value))
            {
                throw new Exception($"{Measurement} tag {name} the value is null or empty.");
            }
            Tags ??= [];
            Tags.Add(name, value);
        }

        public void AddField(string name, object value)
        {
            if (value == null)
            {
                throw new Exception($"{Measurement} field {name} the value is null.");
            }
            Fields ??= [];
            Fields.Add(name, value);
        }
    }
}
